from src.globle import *
from fuc_desktop import *
from fuc_server import *

class KyToolsMenu:
    footer = f"温馨提示:如果你不想执行选项...按{COLOR_MAGENTA} q {COLOR_NONE}或者{COLOR_MAGENTA} Ctrl + C {COLOR_NONE}即可退出\n"

    def __init__(self, title:str="main"):
        self.title = title
        self.header = []
        self.options = []
        self.submenus = []
        self.parent = None
        self.func = None

    def set_parent(self, menu):
        self.parent = menu
    
    def add_submenu(self, menu):
        menu.set_parent(self)
        self.submenus.append(menu)
    
    def add_header(self, header:str):
        self.header = header

    def show_submenu(self):
        if self.parent is None:
            print(f"{COLOR_GREEN} 0. {COLOR_RED} 按 0 退出{COLOR_NONE}\n")
        else:
            print(f"{COLOR_GREEN} 0. {COLOR_RED} 按 0 返回上一层目录{COLOR_NONE}\n")
        for i in range(len(self.submenus)):
            print(f"{COLOR_GREEN} {i+1}. {COLOR_RED} 按 {i+1} {self.submenus[i].title} {COLOR_NONE}\n", )

    def to_next(self, index:int):
        index = int(index) - 1
        return self.submenus[index]

    def do(self, func):
        self.func = func
    
    def print_header(self):
        for option in self.header:
            print(f"{COLOR_BLUE}{option}{COLOR_NONE}\n")
    
    def print_footer(self):
        print(self.footer)

    def show(self):
        CLEAR_SCREEN['clear']()
        self.print_header()
        self.show_submenu()
        self.print_footer()

class KyMainMenu:
    mainmenu = KyToolsMenu()
    system_messages_menu = KyToolsMenu("查看系统信息 -- 系统基本信息、实时网速等")
    system_messages_menu.add_header([".......................系统相关信息.........................."])
    system_messages_menu.add_submenu(KyToolsMenu("查询系统信息"))
    system_messages_menu.add_submenu(KyToolsMenu("查看实时网速"))
    system_repair_menu = KyToolsMenu("修复系统问题 -- 激活问题、桌面无状态栏、图标等")

    main_header=[
        ".....................系统环境异常修复工具.........................",
        "...........我的电脑右键属性--FT2000属于ARM、3A4000属于mips........"
    ]
    mainmenu.add_header(main_header)
    mainmenu.add_submenu(system_messages_menu)
    mainmenu.add_submenu(KyToolsMenu("修复系统问题 -- 激活问题、桌面无状态栏、图标等"))
    mainmenu.add_submenu(KyToolsMenu("修复软件源、软件少、软件无法安装、软件商店报错等问题"))
    mainmenu.add_submenu(KyToolsMenu("软件相关 -- 微信、远程、字体、金山等"))
    mainmenu.add_submenu(KyToolsMenu("查找文件或安装包"))
    mainmenu.add_submenu(KyToolsMenu("打印机相关"))
    mainmenu.add_submenu(KyToolsMenu("上传文件到FTP"))
    mainmenu.add_submenu(KyToolsMenu("服务器相关"))

    

    def __init__(self) -> None:
        self.current_menu = None
    
    def run(self):
        self.current_menu = self.mainmenu
        while True:
            try:
                if len(self.current_menu.submenus) == 0:
                    self.current_menu.func()
                    break
                self.current_menu.show()
                choose = input("请选择菜单 [0-{}]: ".format(len(self.current_menu.submenus)))
                if not choose:
                    continue
                elif choose.isdigit():
                    if choose == "0":
                        if self.current_menu.parent is None:
                            break
                        self.current_menu = self.current_menu.parent
                        continue
                    elif 0 < int(choose) <= len(self.current_menu.submenus):
                        self.current_menu = self.current_menu.to_next(int(choose))
                    else:
                        print(f"\n{COLOR_RED}无效的选择{COLOR_NONE}\n")
                        get_pause()
                elif choose == 'q':
                    exit(0)
                else:
                    print(f"\n{COLOR_RED}无效的选择{COLOR_NONE}\n")
                    get_pause()
            except KeyboardInterrupt as error:
                break

    